package com.it.hanzhongtravelv1.controller;

import com.it.hanzhongtravelv1.dto.FoodDTO;
import com.it.hanzhongtravelv1.dto.RestaurantDTO;
import com.it.hanzhongtravelv1.entity.Food;
import com.it.hanzhongtravelv1.entity.Restaurant;
import com.it.hanzhongtravelv1.service.FoodRecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class FoodController {

    private final FoodRecommendationService foodService;

    @Autowired
    public FoodController(FoodRecommendationService foodService) {
        this.foodService = foodService;
    }

    // Food endpoints
    @GetMapping("/foods")
    public List<FoodDTO> getAllFoods() {
        return foodService.getAllFoods().stream()
                .map(this::convertToFoodDTO)
                .collect(Collectors.toList());
    }

    @PostMapping("/foods")
    public ResponseEntity<FoodDTO> addFood(@RequestBody FoodDTO foodDTO) {
        Food food = convertToFoodEntity(foodDTO);
        Food savedFood = foodService.saveFood(food);
        return ResponseEntity.ok(convertToFoodDTO(savedFood));
    }

    @PutMapping("/foods/{id}")
    public ResponseEntity<FoodDTO> updateFood(@PathVariable Long id, @RequestBody FoodDTO foodDTO) {
        foodDTO.setId(id);
        Food food = convertToFoodEntity(foodDTO);
        Food updatedFood = foodService.saveFood(food);
        return ResponseEntity.ok(convertToFoodDTO(updatedFood));
    }

    @DeleteMapping("/foods/{id}")
    public ResponseEntity<Void> deleteFood(@PathVariable Long id) {
        foodService.deleteFood(id);
        return ResponseEntity.noContent().build();
    }

    // Restaurant endpoints
    @GetMapping("/restaurants")
    public List<RestaurantDTO> getAllRestaurants() {
        return foodService.getAllRestaurants().stream()
                .map(this::convertToRestaurantDTO)
                .collect(Collectors.toList());
    }

    @PostMapping("/restaurants")
    public ResponseEntity<RestaurantDTO> addRestaurant(@RequestBody RestaurantDTO restaurantDTO) {
        Restaurant restaurant = convertToRestaurantEntity(restaurantDTO);
        Restaurant savedRestaurant = foodService.saveRestaurant(restaurant);
        return ResponseEntity.ok(convertToRestaurantDTO(savedRestaurant));
    }

    @PutMapping("/restaurants/{id}")
    public ResponseEntity<RestaurantDTO> updateRestaurant(@PathVariable Long id, @RequestBody RestaurantDTO restaurantDTO) {
        restaurantDTO.setId(id);
        Restaurant restaurant = convertToRestaurantEntity(restaurantDTO);
        Restaurant updatedRestaurant = foodService.saveRestaurant(restaurant);
        return ResponseEntity.ok(convertToRestaurantDTO(updatedRestaurant));
    }

    @DeleteMapping("/restaurants/{id}")
    public ResponseEntity<Void> deleteRestaurant(@PathVariable Long id) {
        foodService.deleteRestaurant(id);
        return ResponseEntity.noContent().build();
    }
    
    // Conversion methods
    private FoodDTO convertToFoodDTO(Food food) {
        return new FoodDTO(food);
    }

    private Food convertToFoodEntity(FoodDTO dto) {
        Food food = new Food();
        if (dto.getId() != null) {
            food.setId(dto.getId());
        }
        food.setName(dto.getName());
        food.setDescription(dto.getDescription());
        food.setPrice(dto.getPrice());
        food.setSpecialFeature(dto.getSpecialFeature());
        food.setImageUrl(dto.getImageUrl());
        food.setRating(dto.getRating());
        food.setTags(dto.getTags());
        food.setCategory(dto.getCategory());
        return food;
    }

    private RestaurantDTO convertToRestaurantDTO(Restaurant restaurant) {
        return new RestaurantDTO(restaurant);
    }

    private Restaurant convertToRestaurantEntity(RestaurantDTO dto) {
        Restaurant restaurant = new Restaurant();
        if (dto.getId() != null) {
            restaurant.setId(dto.getId());
        }
        restaurant.setName(dto.getName());
        restaurant.setAddress(dto.getAddress());
        restaurant.setPhone(dto.getPhone());
        restaurant.setRating(dto.getRating());
        restaurant.setSpecialtyDish(dto.getSpecialtyDish());
        restaurant.setAveragePrice(dto.getAveragePrice());
        restaurant.setOpeningHours(dto.getOpeningHours());
        return restaurant;
    }
} 